package org.guajava.controller.system;

import org.guajava.common.annotation.Log;
import org.guajava.common.base.AjaxResult;
import org.guajava.common.base.ResultInfo;
import org.guajava.common.enums.BusinessType;
import org.guajava.domain.SysMenu;
import org.guajava.domain.SysRole;
import org.guajava.domain.SysUser;
import org.guajava.framework.util.ShiroUtils;
import org.guajava.framework.web.base.BaseController;
import org.guajava.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 *
 * @author yuewei
 */
@Controller
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService menuService;
    private String prefix = "system/menu";
    @RequestMapping("/left")
    @ResponseBody
    public ResultInfo<List<SysMenu>> getMenuInfoList(@RequestParam("menuId") Integer menuId) {
        Map<String,Object> param = new HashMap<String, Object>();
        List<SysMenu> subMenuInfoList = new ArrayList<>();
        // 取身份信息
        SysUser user = getSysUser();
        param.put("user",user);
        param.put("parentId",menuId);  //根目录的id
            //根据顶级目录的id获取该用户下相应的子级菜单
        subMenuInfoList =  menuService.selectSubMenusByTopMenuId(param);
       return new ResultInfo<>(subMenuInfoList);
    }

    @GetMapping()
    public String menu()
    {
        return prefix + "/menu";
    }

    /**
     * 获取菜单列表（菜单管理）
     * @param menu
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultInfo<List<SysMenu>> list(SysMenu menu)
    {
        List<SysMenu> menuList = menuService.selectMenuList(menu);
        return new ResultInfo<>(menuList);
    }


    /**
     * 新增保存菜单
     */
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMenu menu)
    {
        menu.setCreateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(menuService.insertMenu(menu));
    }


    /**
     * 修改菜单
     */
    @GetMapping("/edit/{menuId}")
    public String edit(@PathVariable("menuId") Long menuId, ModelMap mmap)
    {
        mmap.put("menu", menuService.selectMenuById(menuId));
        return prefix + "/edit";
    }


    /**
     * 修改保存菜单
     */
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMenu menu)
    {
        menu.setUpdateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(menuService.updateMenu(menu));
    }


    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Map<String, Object>> roleMenuTreeData(SysRole role)
    {
        List<Map<String, Object>> tree = menuService.roleMenuTreeData(role);
        return tree;
    }

    @RequestMapping("/editForm")
    public String addMenu(Model model) {
        return "system/menu/menuForm.html";
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    @ResponseBody
    public List<Map<String, Object>> menuTreeData(SysRole role)
    {
        List<Map<String, Object>> tree = menuService.menuTreeData();
        return tree;
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") Long menuId, ModelMap mmap)
    {
        mmap.put("menu", menuService.selectMenuById(menuId));
        return prefix + "/menuTree";
    }
}
